<?php
class CF_Filtering{

	function CF_Filtering($fieldspec){
		$this->fieldspec = $fieldspec;
	}

	var $selfpage;
	var $fieldspec;
	/**
	* @var string $filterprefix prefix for filtered variables (column name used)
	*/
	var $filterprefix = 'flt_';	
	/**
	* @var array $filters Holds fitlers as array filers[<column>] = <value> filled by get_filter_vals().  This expects filter variables form the $_REQUEST or from the $_SESSION if saved (pass empty to clear).  Uses prefix determined by $filterprefix
	*/
	var $filters; 
	/**
	* @var bool $multiselect Determines whether to  implement multi-change logic on table
	*/
	/**
	* @var bool $show_filter_submit show sumbit filter (Default true)
	*/   	
    var $show_filter_submit = true;
	/**
	* @var string $show_filter_value string to show on filter button (default 'Apply')
	*/   	
	var $show_filter_value = 'Apply';
	/**
	* @var string $addmarkup string of markup to put in add form for other hidden fields, etc.
	*/  	
	

	/**
	 * get_filter_vals() will recieve the filter variables from Session or request
     * and set them in the filters array.
     * To clear filter, pass in a request with the filter set to nothing
     * ($variable1=&variable2=)
     * Filters are kept as filter [filter variable name][value]
     * Where filter variable name is filter prefix ($this->filterprefix) . variable name
	 */
	function get_filter_vals($method = 'req'){
		//print "IN GET FILTER VALS";
		foreach($this->fieldspec AS $col_name => $colspec){
			//if(isset($colspec['filter'])){
				// CT 20090618 take out if this works
				//print "Getting $col_name Filter";
                $currentvalue = GetPersistent($this->filterprefix . $col_name,$method);
                if(!is_null($currentvalue)){
                    $this->filters[$this->filterprefix . $col_name]['var'] = $col_name;
                    $this->filters[$this->filterprefix . $col_name]['val'] = $currentvalue;
                   // $this->filters[$this->filterprefix . $col_name]['source'] =
                }else{
					unset($this->filters[$this->filterprefix . $col_name]['var']);
					unset($this->filters[$this->filterprefix . $col_name]['val']);
				}
           // }
        }
    }

	/**
	 * get_filter_pvals() will recieve the filter variables from Session or request
     * and set them in the filters array.  This uses the page specific filters storage
     * To clear filter, pass in a request with the filter set to nothing
     * ($variable1=&variable2=)
     * Filters are kept as filter [filter variable name][value]
     * Where filter variable name is filter prefix ($this->filterprefix) . variable name
	 */	
	function get_filter_pvals($method = 'req'){
		foreach($this->fieldspec AS $col_name => $colspec){
			//if(isset($colspec['filter'])){
                $currentvalue = GetPagePersistent($this->filterprefix . $col_name,$method);
                if(!is_null($currentvalue)){
                    $this->filters[$this->filterprefix . $col_name]['var'] = $col_name;
                    $this->filters[$this->filterprefix . $col_name]['val'] = $currentvalue;
                   // $this->filters[$this->filterprefix . $col_name]['source'] =
                }else{
					unset($this->filters[$this->filterprefix . $col_name]['var']);
					unset($this->filters[$this->filterprefix . $col_name]['val']);
				}
            //}
        }
    }	
	
	//Set filter and Session value - generic	
	function set_filter_val($col_name, $filter_value, $page_name = ''){
		if($page_name == ''){
			$page_name = $this->selfpage;
		}
		$this->filters[$this->filterprefix . $col_name]['var'] = $col_name;
		$this->filters[$this->filterprefix . $col_name]['val'] = $filter_value;
		$_SESSION[$this->filterprefix . $col_name] = $filter_value;
	}

	//UnSet filter and Session value - generic	
	function un_set_filter_val($col_name, $page_name = ''){
		if($page_name == ''){
			$page_name = $this->selfpage;
		}
		unset($this->filters[$this->filterprefix . $col_name]);
		unset($_SESSION[$this->filterprefix . $col_name]);
	}
	//Set filter and Session value - page specific	
	function set_filter_pval($col_name, $filter_value, $page_name = ''){
		if($page_name == ''){
			$page_name = $this->selfpage;
		}
		$this->filters[$this->filterprefix . $col_name]['var'] = $col_name;
		$this->filters[$this->filterprefix . $col_name]['val'] = $filter_value;
		$_SESSION[$page_name]['filters'][$this->filterprefix . $col_name] = $filter_value;
	}

	//UnSet filter and Session value - page specific	
	function un_set_filter_pval($col_name, $page_name = ''){
		if($page_name == ''){
			$page_name = $this->selfpage;
		}
		unset($this->filters[$this->filterprefix . $col_name]);
		unset($_SESSION[$page_name]['filters'][$this->filterprefix . $col_name]);
	}


	
	/**
	 * make_query_filter will create a query filter according to fieldspec settings, Request input, and Session data
	 * 
	 */
	function make_query_filter(){

		if(!empty( $this->filters)) {
            $tabledot = '';
			foreach($this->filters AS $filtername => $filter){
	// CT 2009-10-27 Added ' AND (!empty($this->fieldspec[$currentvar]['filter']))
	// CT 2009-11-07 Added this as new if stmt to the correct line a few lines below
                $currentvar = $filter['var'];
				if(!empty($this->fieldspec[$currentvar]['filter'])){
					if(isset($this->fieldspec[$currentvar]['source'])){
							$tabledot = $this->fieldspec[$currentvar]['source'] . '.';
						}
					$flt_text .= " AND $tabledot" . $filter['var'] . " = '" . $filter['val'] . "' ";
				}
			}	
		}
	
	return $flt_text;
	}
	
	//MOVE TO CF_FILTER
	/**
	 *
	 * make_filters_box  Method to create a fitlers box which will show each editable
	  * value in it's own text entry or drop-down box for filtering
	 * @param string $method Form Method (get or post)
	 *
	 */
    function make_filters_box($method = 'get'){
        $none_entry[1]['keyvalue'] = '';
		$none_entry[1]['showvalue'] = '';
		$showbox = false;
		
		
		$filterbox .= "<table class='filterbox'><form name='filters_box' action='$this->selfpage' method='$method'>\n";
        $count = 1;
        foreach($this->fieldspec AS $col_name => $colspec){
			if((!empty($colspec['filter']) ) AND (!empty($colspec['showfilter']))){ // insert AND 
				$showbox = true;
                if(!empty($colspec['headname'])){
                    $elements[$count]['head'] = $colspec['headname'] ;
                }else{
                    $elements[$count]['head'] =  $col_name;
                }
				$filtername = $this->filterprefix . $col_name;
                $elements[$count]['body'] = MakeEditField($this->fieldspec[$col_name], $filtername, $this->filters[$filtername]['val'], 'this.form.submit()', $filtername, $none_entry,$this->top_dir);
                $count ++;
			}// end colpsec filter set
        }// end foreach field
        $filterbox .= $this->make_element_set($elements, "class='filter_head'","class='filter_body'");
        if($this->show_filter_submit){
            $filterbox .= "<tr class='filter_body' ><td colspan='$count' > <input type='hidden' name='$this->startatvar' value='0'> <input type='submit' id='refresh' name='refresh' value='$this->show_filter_value'></form> </td></tr>\n";
        }else{
            $filterbox .= "</form>";
         }
         $filterbox .= "</table>";
	if($showbox){	 
		return $filterbox;
	}
	else{
		return '';
	}
    }
	/**
	 *
     * @make_element set will create a line of headed elements arranged in a table
     * from an input of an element_set array.  Each row has element_name and value
     * @param array elements An array with [body] and [head] parts where head
     * elements['head'] is the title and elements['body'] is what to put in the cell
     * @param string $headset Settings for heading elements (class name, etc)
     * @param string $bodyset Settings for body elemetns (class naem, etc)
	 *
	 */
    function make_element_set($elements, $headset, $bodyset){

        if(is_array($elements)){
        foreach ($elements AS $current){
            $headtext .= "<th>" . $current['head'] . "</th>\n";
            $bodytext .=  "<td>" . $current['body'] . "</td>\n";
        }
        }
        return "<tr $headset>$headtext </tr> <tr $bodyset>$bodytext</tr>";
    }
	
	
}

?>